package com.arpa.wms.controller;

import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.consts.TipConst;
import com.arpa.wms.domain.dto.CustomerDTO;
import com.arpa.wms.domain.entity.Customer;
import com.arpa.wms.domain.vo.CustomerVO;
import com.arpa.wms.domain.vo.WarehouseVO;
import com.arpa.wms.service.ICustomerService;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 客户 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-09-04
 */
@Api(tags = "客户")
@RestController
@RequestMapping(value="/customer", produces = "application/json;charset=UTF-8")
@Log4j2
public class CustomerController extends BaseController {

   @Autowired
   private ICustomerService iCustomerService;

    /**Goo
     * 获取 客户 列表
     *
     * @param customerDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 客户 列表", notes = "获取 客户 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = CustomerVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(CustomerDTO customerDTO,HttpServletRequest request) {
        customerDTO.setGroupCode(UserUtil.getBranchCode());
        try {
            //调整排序字段
            customerDTO = iCustomerService.adjustSortFields(customerDTO);
            CustomerVO customerVOSum = iCustomerService.queryListSum(customerDTO);
            List<CustomerVO> customerVOList = iCustomerService.queryList(customerDTO);

            Page returnPage = new Page(customerDTO.getPageNum(),customerDTO.getPageSize());
            returnPage.setRecords(customerVOList);
            returnPage.setTotal(customerVOSum.getTotal());
            returnPage.setSumObject(customerVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取客户审核列表
     *
     * @param customerDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取客户审核列表", notes = "获取客户审核列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = CustomerVO.class))
    @NeedLogin
    @GetMapping("/approveList")
    public Result approveList(CustomerDTO customerDTO, HttpServletRequest request) {
        try {
            customerDTO.setGroupCode(UserUtil.getBranchCode());
            CustomerVO customerVOSum = iCustomerService.queryListSum(customerDTO);
            List<CustomerVO> customerVOList = iCustomerService.queryList(customerDTO);

            Page returnPage = new Page(customerDTO.getPageNum(),customerDTO.getPageSize());
            returnPage.setRecords(customerVOList);
            returnPage.setTotal(customerVOSum.getTotal());
            returnPage.setSumObject(customerVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 客户
     *
     * @param entity Customer
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 客户 ", notes = "添加 客户 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(@RequestBody @Validated(Customer.Save.class) Customer entity, HttpServletRequest request) {
        try {
            entity.setGroupCode(UserUtil.getBranchCode());
            iCustomerService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取客户详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取客户详情", notes = "获取客户详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "客户code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Customer.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result customer(@PathVariable String code, HttpServletRequest request) {
        try {
            CustomerVO entity = iCustomerService.getOneInfo(code);
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改客户
     *
     * @param entity 客户
     * @param request
     * @return
     */
    @ApiOperation(value = "修改客户", notes = "修改客户")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(@RequestBody @Validated(CustomerDTO.Update.class) CustomerDTO entity, HttpServletRequest request) {
        if(StringUtils.isBlank(entity.getCode())){
            return Result.error(TipConst.CODE_NOT_EMPTY);
        }
        int updateNum = iCustomerService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 客户
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 客户 ", notes = "删除 客户 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "/batchdel")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iCustomerService.batchDelete(codes,UserUtil.getCode());
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取 客户下拉列表数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 客户下拉列表数据", notes = "获取 客户下拉列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = WarehouseVO.class))
    @NeedLogin
    @GetMapping("/chooseData")
    public Result chooseData(
            HttpServletRequest request) {
        try {
            List<CustomerVO> customerVOS = iCustomerService.findCustomer();
            return Result.ok().data(customerVOS);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 客户信息审核不通过
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "客户信息审核不通过", notes = "客户信息审核不通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("/approveFail/{code}")
    public Result approveFail(@PathVariable String code,HttpServletRequest request) {
        if(StringUtils.isBlank(code)){
            return Result.error(TipConst.CODE_NOT_EMPTY);
        }
        boolean updateNum = iCustomerService.approve(code,2);
        if(updateNum) {
            return Result.ok("客户信息审核成功！");
        }else{
            return Result.error("客户信息审核失败！");
        }
    }

    /**
     * 客户信息审核通过
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "客户信息审核通过", notes = "客户信息审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("/approvePass/{code}")
    public Result approvePass(@PathVariable String code ,HttpServletRequest request) {
        if(StringUtils.isBlank(code)){
            return Result.error(TipConst.CODE_NOT_EMPTY);
        }
        boolean updateNum = iCustomerService.approve(code,1);
        if(updateNum) {
            return Result.ok("客户信息审核成功！");
        }else{
            return Result.error("客户信息审核失败！");
        }
    }



 }
